import { ref } from 'vue'
import {
  customFiledSave,
  getCustomFieldList,
  getReturnFactoryCustomFieldDetail,
  returnFactoryCustomFieldsDel,
  returnFactoryCustomFieldsMobileShow,
  returnFactoryCustomFieldsRequired,
  returnFactoryCustomFieldsUpdateStatus,
} from '@/api/return-factory'

export const useCustomFieldForm = () => {
  const saveLoading = ref(false)
  const form = ref<ReturnFactoryAPIRequest.CustomFileForm>({
    delFieldContentIds: [],
    fieldContentVos: [],
    delRepairModuleFieldRelationIds: [],
    id: '',
    mobileShow: true,
    name: '',
    repairModuleFieldRelationVos: [],
    required: true,
    status: false,
    tip: '',
    type: 1,
    uploadQty: 10,
    uploadSize: 100,
  })

  const save = async () => {
    saveLoading.value = true
    const res = await customFiledSave(form.value)
    saveLoading.value = false
    return res.data
  }

  const resetForm = () => {
    form.value = {
      delFieldContentIds: [],
      fieldContentVos: [],
      delRepairModuleFieldRelationIds: [],
      id: '',
      mobileShow: true,
      name: '',
      repairModuleFieldRelationVos: [],
      required: true,
      status: false,
      tip: '',
      type: 1,
      uploadQty: 10,
      uploadSize: 100,
    }
  }

  return {
    form,
    saveLoading,
    save,
    resetForm,
  }
}

export const useGetCustomFieldList = () => {
  const getListLoading = ref(false)
  const getListParam = ref({
    name: '',
    page: 1,
    limit: 50,
  })
  const list = ref<Array<ReturnFactoryCustomField>>([])
  const total = ref(0)

  const getList = async () => {
    getListLoading.value = true
    const res = await getCustomFieldList(getListParam.value)
    getListLoading.value = false
    list.value = res.data.data.rows
    total.value = Number(res.data.data.total)
  }

  return {
    list,
    total,
    getListParam,
    getListLoading,
    getList,
  }
}

export const useGetFieldDetail = () => {
  const getFieldDetailLoading = ref(false)
  const fieldDetail = ref<ReturnFactoryCustomFieldDetail>({
    delFieldContentIds: [],
    delRepairModuleFieldRelationIds: [],
    fieldContentVos: [],
    id: '',
    mobileShow: true,
    name: '',
    repairModuleFieldRelationVos: [],
    required: true,
    status: false,
    tip: '',
    type: 1,
    uploadQty: 10,
    uploadSize: 100,
  })

  const getFieldDetail = async (id: string) => {
    getFieldDetailLoading.value = true
    const res = await getReturnFactoryCustomFieldDetail(id)
    getFieldDetailLoading.value = false
    fieldDetail.value = res.data.data.row
  }

  return {
    fieldDetail,
    getFieldDetailLoading,
    getFieldDetail,
  }
}

export const useFieldsDel = () => {
  const loading = ref(false)

  const del = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsDel(ids)
    loading.value = false
    return res.data
  }

  return {
    loading,
    del,
  }
}

export const useFieldsDisable = () => {
  const loading = ref(false)

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsUpdateStatus(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    disable,
  }
}

export const useFieldsEnable = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsUpdateStatus(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
  }
}

export const useFieldsMobileShowOrHide = () => {
  const loading = ref(false)

  const show = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsMobileShow(ids, true)
    loading.value = false
    return res.data
  }

  const hide = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsMobileShow(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    show,
    hide,
  }
}

export const useFieldsRequired = () => {
  const loading = ref(false)

  const required = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsRequired(ids, true)
    loading.value = false
    return res.data
  }

  const notRequired = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFieldsRequired(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    required,
    notRequired,
  }
}
